package com.atom.common.delegate;

import java.util.Date;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.task.IdentityLink;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.atom.common.DelegateType;
import com.atom.common.VariablesConstant;
import com.atom.common.activiti.ActivitiHandler;
import com.atom.common.util.JsonUtil;

/**
 * Title: TaskCreateDelegateHandler
 * Description: 创建任务时写平台待办表
 * @author WEWE
 * @date 2017年12月5日 上午11:33:19
 * @version 1.0
 */
@Component
@Transactional
public class TaskCreateDelegateHandler {

  private static Logger logger = Logger.getLogger(TaskCreateDelegateHandler.class);

  @Resource DelegateInfoDao delegateInfoDao;
  @Resource DelegateLogDao delegateLogDao;
  @Resource ActivitiHandler activitiHandler;

  public String[] execute(DelegateTask task, Map<String, Object> variables, String owner) {
    String[] users = null;
    String processDefinitionKey = (String) variables.get(VariablesConstant.PROCESS_DEFINITION_KEY);
    Date current = new Date();
    DelegateInfo info = delegateInfoDao.findByUserByPDK(owner, processDefinitionKey, current);
    if (info != null) {
      logger.info("DELEGATEINFO:ID:" + info.getId());
      // 代理人
      String assignee = info.getAssignee();
      // 代理类型
      int delegateType = info.getDelegateType();
      switch (delegateType) {
        case DelegateType.COOP : {
          if (taskHasUser(task, assignee)) {
            users = new String[1];
            users[0] = owner;
            break;
          } else {
            task.addCandidateUser(assignee);
            // 记录操作日志
            DelegateLog log = new DelegateLog(info.getId(), task.getId(), owner, assignee, DelegateType.COOP);
            delegateLogDao.save(log);
            users = new String[2];
            users[0] = owner;
            users[1] = assignee;
            break;
          }
        }
        case DelegateType.OUT : {
          task.deleteCandidateUser(owner);
          task.addCandidateUser(assignee);
          // 记录操作日志
          DelegateLog log = new DelegateLog(info.getId(), task.getId(), owner, assignee, DelegateType.OUT);
          delegateLogDao.save(log);
          users = new String[1];
          users[0] = assignee;
          break;
        }
      }
    } else {
      users = new String[1];
      users[0] = owner;
    }
    logger.info("USERS:" + JsonUtil.ObjectToJsonQuietly(users));
    return users;
  }

  private boolean taskHasUser(DelegateTask task, String assignee) {
    if (assignee.equals(task.getAssignee())) {
      logger.info(task.getId() + " task has user true" + assignee);
      return true;
    }
    Set<IdentityLink> ll = task.getCandidates();
    for (IdentityLink l : ll) {
      if (assignee.equals(l.getUserId())) {
        logger.info(task.getId() + " task has user true" + assignee);
        return true;
      }
    }
    logger.info(task.getId() + " task has user false" + assignee);
    return false;
  }

}
